package com.cwx.mychat.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.UserContactTypeEnum;
import com.cwx.mychat.entity.po.ChatMessage;
import com.cwx.mychat.entity.po.ChatSession;
import com.cwx.mychat.entity.po.ChatSessionUser;
import com.cwx.mychat.mappers.ChatSessionMapper;
import com.cwx.mychat.service.ChatSessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 作者： cwx
 * 时间： 2024/9/21 14:36
 * 版本： 1.0
 * 注释：
 */
@Slf4j
@Service
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession> implements ChatSessionService {


    @Autowired
    private ChatSessionMapper chatSessionMapper;

    //更新会话
    public ChatSession updateSession(ChatMessage chatMessage, TokenUserInfoDto tokenUserInfoDto, UserContactTypeEnum userContactTypeEnum){
        ChatSession chatSession = new ChatSession();
        chatSession.setSessionId(chatMessage.getSessionId());
        chatSession.setLastMessage(chatMessage.getMessageContent());
        if(UserContactTypeEnum.GROUP == userContactTypeEnum){
            chatSession.setLastMessage(tokenUserInfoDto.getNickName() + ": " + chatMessage.getMessageContent());
        }
        chatSession.setLastReceiveTime(new Date().getTime());
        int update = chatSessionMapper.updateById(chatSession);
        if(update < 0){
            return null;
        }
        return chatSession;
    }

    @Override
    public boolean insertOrUpdateSession(ChatSessionUser chatSessionUser) {
        ChatSession chatSession = new ChatSession();
        chatSession.setSessionId(chatSessionUser.getSessionId());
        chatSession.setCreateTime(new Date());
        chatSession.setLastReceiveTime(new Date().getTime());
        int c = chatSessionMapper.insertOrUpdateChatSession(chatSession);
        if(c == 0){
            log.info("插入或更新会话最终语句没做任何操作");
        }else if(c == 1){
            log.info("插入会话最总语句成功, 没有发生更新");
        }else if(c == 2){
            log.info("更新会话最终语句成功");
        }
        return c >= 0;
    }

    @Override
    public boolean insertOrUpdateSession(ChatSessionUser chatSessionUser, String helloMsg) {
        ChatSession chatSession = new ChatSession();
        chatSession.setSessionId(chatSessionUser.getSessionId());
        chatSession.setCreateTime(new Date());
        chatSession.setLastMessage(helloMsg);
        chatSession.setLastReceiveTime(new Date().getTime());
        int c = chatSessionMapper.insertOrUpdateChatSession(chatSession);
        if(c == 0){
            log.info("插入或更新会话最终语句没做任何操作");
        }else if(c == 1){
            log.info("插入会话最总语句成功, 没有发生更新");
        }else if(c == 2){
            log.info("更新会话最终语句成功");
        }
        return c >= 0;
    }
}
